<?php

class Uploads
{

    //生成文件名字
    public static function random_filename($filename)
    {
        $getExt = explode('.', $filename);
        $file_ext = $getExt[count($getExt) - 1];
        $new_filename = time() . rand(0, 99999999999) . "." . $file_ext;
        return $new_filename;
    }

    //生成文件路径
    public static function directory($dir)
    {
        return is_dir($dir) or (self::directory(dirname($dir)) and mkdir($dir, 0777));
    }

    //生成路径
    public static function creatpath($folder)
    {
        $new_targetPath = $folder . '/' . date('Y', time()) . '/' . date('m', time()) . '/';
        $targetPath = $_SERVER['DOCUMENT_ROOT'] . $new_targetPath;
        $flag = self::directory($targetPath);
        return $targetPath;
    }

    //生成一个文件
    public static function creatfile($dir, $filename, $contents = '')
    {
        $f = $dir . DIRECTORY_SEPARATOR . $filename;
        if (false != ($fp = @fopen($f, 'wb')))
        {
            fwrite($fp, $contents);
            fclose($fp);
            return true;
        }
        else
        {
            $rs = 'Unable to create file';
            return false;
        }
    }

    //$uploadfile  路径  $filearray文件数组
    //处理在同一个数据记录时候图片的上传
    public static function handleupload($uploadfile, $filearray)
    {
        $filename = self::random_filename($filearray['name']);
        $directory = self::creatpath($uploadfile);
        Upload::save($filearray, $filename, $directory);
        $savepath = $directory . $filename;
        $savepath = explode($_SERVER['DOCUMENT_ROOT'], $savepath);
        return $savepath[1];
    }

}

?>
